﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class MultiWarehouse : BasePage
    {
        protected Button btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnNew;
        protected Button btnSave;
        protected DropDownList ddlLevelNum;
        protected HtmlForm F1060;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected HtmlInputHidden hidAction;
        protected HtmlInputHidden hidOldDeptCode;
        protected HtmlInputHidden hidParentText;
        protected HtmlInputHidden hidParentValue;
        protected HtmlInputHidden hidSelectedNodeText;
        protected HtmlInputHidden hidSelectedNodeValue;
        protected HtmlInputHidden hidType;
        protected Label Label18;
        protected Label Label2;
        protected Label lblDescription;
        protected Label lblDominaID;
        protected Label lblDominaName;
        protected Label lblLevelNum;
        protected Label lblMsg;
        protected Label lblRole;
        protected HtmlLink mycss;
        protected ScriptManager ScriptManager1;
        protected TreeView TreeView1;
        protected TextBox txtDescription;
        protected TextBox txtDomainID;
        protected TextBox txtDominaName;
        protected TextBox txtParentCompany;
        protected UpdatePanel UpdatePanel1;

        private void AddNodes(TreeView tree, DomainInfo[] domainList)
        {
            try
            {
                int i;
                Hashtable htTreeMapping = new Hashtable();
                for (i = 0; i < domainList.Length; i++)
                {
                    DomainInfo domain = domainList[i];
                    TreeNode node = new TreeNode(domain.Name, domain.DomainID.ToString()) {
                        Target = domain.ParentDomainID.ToString(),
                        SelectAction = TreeNodeSelectAction.SelectExpand
                    };
                    htTreeMapping.Add(domain.DomainID.ToString(), node);
                }
                for (i = 0; i < domainList.Length; i++)
                {
                    string parentCode = domainList[i].ParentDomainID.ToString();
                    TreeNode subNode = (TreeNode) htTreeMapping[domainList[i].DomainID.ToString()];
                    if (parentCode != "-1")
                    {
                        TreeNode parentNode = (TreeNode) htTreeMapping[parentCode];
                        parentNode.ChildNodes.Add(subNode);
                    }
                    else
                    {
                        tree.Nodes.Add((TreeNode) htTreeMapping[subNode.Value]);
                    }
                }
                this.ddlDataBind(1);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            DomainInfo[] domainList = base.PageQueryEntitySession as DomainInfo[];
            for (int i = 0; domainList.Count<DomainInfo>() > i; i++)
            {
                if (domainList[i].ParentDomainID.ToString() == this.txtDomainID.Text)
                {
                    this.lblMsg.Text = string.Format(base.GetGlobalMsgResource("F1600_006"), this.txtDominaName.Text);
                    return;
                }
            }
            try
            {
                List<string> stringList = new List<string> {
                    this.txtDomainID.Text
                };
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(stringList, "DOMAIN") + base.GetGlobalMsgResource("F1600_007");
                int status = int.Parse(this.ddlLevelNum.SelectedValue) - 1;
                if (status == 0)
                {
                    status = -1;
                }
                this.ddlDataBind(status);
                this.init();
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            this.hidType.Value = "2";
            this.SetBtnEnbled("Edit");
            this.SetTxtEnbled("Edit");
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            this.hidParentText.Value = this.txtDominaName.Text;
            this.hidParentValue.Value = this.txtDomainID.Text;
            this.txtParentCompany.Text = this.txtDominaName.Text;
            this.hidType.Value = "1";
            this.txtDomainID.Text = "";
            this.txtDominaName.Text = "";
            this.txtDescription.Text = "";
            this.SetBtnEnbled("New");
            this.SetTxtEnbled("New");
            this.ddlDataBind(int.Parse(this.ddlLevelNum.SelectedValue));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtParentCompany.Text))
            {
                if (this.ddlLevelNum.SelectedValue == "1")
                {
                    this.txtParentCompany.Text = "1";
                    this.hidParentValue.Value = "-1";
                }
                else
                {
                    this.lblMsg.Visible = true;
                    this.ddlLevelNum.Focus();
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1600_002");
                    return;
                }
            }
            if (string.IsNullOrEmpty(this.txtDominaName.Text))
            {
                this.lblMsg.Visible = true;
                this.txtDominaName.Focus();
                this.lblMsg.Text = base.GetGlobalMsgResource("F1600_003");
            }
            else
            {
                try
                {
                    DomainInfo domain = new DomainInfo();
                    if (!string.IsNullOrEmpty(this.txtDomainID.Text))
                    {
                        domain.DomainID = Convert.ToInt32(this.txtDomainID.Text);
                    }
                    domain.Name = this.txtDominaName.Text;
                    domain.ParentDomainID = Convert.ToInt32(this.hidParentValue.Value);
                    domain.Description = this.txtDescription.Text;
                    if (this.hidType.Value == "1")
                    {
                        domain.LevelNum = Convert.ToInt32(this.ddlLevelNum.SelectedValue);
                        domain.CreatedBy = "dba";
                        domain.CreatedDate = DateTime.Now;
                        domain.UpdatedBy = "dba";
                        domain.UpdatedDate = DateTime.Now;
                        this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(domain, "DOMAIN") + base.GetGlobalMsgResource("F1600_004");
                    }
                    else
                    {
                        domain.LevelNum = Convert.ToInt32(this.ddlLevelNum.SelectedValue) - 1;
                        domain.CreatedBy = "dba";
                        domain.CreatedDate = DateTime.Now;
                        domain.UpdatedBy = "dba";
                        domain.UpdatedDate = DateTime.Now;
                        this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(domain, "DOMAIN") + base.GetGlobalMsgResource("F1600_005");
                    }
                    this.init();
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                    this.lblMsg.Visible = true;
                }
            }
        }

        private void ClearData()
        {
            try
            {
                this.txtDomainID.Text = string.Empty;
                this.txtDominaName.Text = string.Empty;
                this.txtParentCompany.Text = string.Empty;
                this.txtDescription.Text = string.Empty;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        private void ddlDataBind(int status)
        {
            for (int i = 0; this.ddlLevelNum.Items.Count > i; i++)
            {
                if (i <= status)
                {
                    if (this.ddlLevelNum.Items[i].Enabled && (this.ddlLevelNum.Items[i].Value != "4"))
                    {
                        this.ddlLevelNum.Items[i].Enabled = false;
                    }
                }
                else if (!this.ddlLevelNum.Items[i].Enabled)
                {
                    if (this.ddlLevelNum.Items[i].Value == "0")
                    {
                        this.ddlLevelNum.SelectedValue = "0";
                    }
                    this.ddlLevelNum.Items[i].Enabled = true;
                }
            }
        }

        private void init()
        {
            try
            {
                this.SetTxtEnbled("NewLoad");
                this.btnNew.Enabled = false;
                this.btnEdit.Enabled = false;
                this.btnDelete.Enabled = false;
                this.btnSave.Enabled = false;
                this.btnAction.Enabled = false;
                this.ClearData();
                this.InitTree();
                if ((this.TreeView1.DataSource == null) || (this.TreeView1.Nodes[0] == null))
                {
                    this.btnNew.Enabled = true;
                    this.btnSave.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        private void InitTree()
        {
            try
            {
                this.TreeView1.Nodes.Clear();
                DataSet ds = BaseInfoServiceClientProxy.GetInstance().GetAllInfo("DOMAIN");
                if (ds.Tables[0].Rows.Count != 0)
                {
                    DomainInfo[] domainList = new DomainInfo[ds.Tables[0].Rows.Count];
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr = ds.Tables[0].Rows[i];
                        domainList[i] = new DomainInfo { RowID = Convert.ToInt32(dr["ROWID"]), WhID = dr["WH_ID"].ToString(), DomainID = Convert.ToInt32(dr["DOMAIN_ID"]), Name = dr["NAME"].ToString(), Description = dr["DESCRIPTION"].ToString(), ParentDomainID = Convert.ToInt32(dr["PARENT_DOMAIN_ID"]), LevelNum = Convert.ToInt32(dr["LEVEL_NUM"]), CreatedBy = dr["CREATED_BY"].ToString(), CreatedDate = Convert.ToDateTime(dr["CREATED_DATE"]), UpdatedBy = dr["UPDATED_BY"].ToString(), UpdatedDate = Convert.ToDateTime(dr["UPDATED_DATE"]) };
                    }
                    base.PageQueryEntitySession = domainList;
                    this.AddNodes(this.TreeView1, domainList);
                    if ((this.TreeView1.Nodes.Count > 0) && (this.TreeView1.Nodes[0] != null))
                    {
                        this.TreeView1.Nodes[0].Expand();
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.Response.Expires = 0;
                this.init();
            }
            this.lblMsg.Text = "";
        }

        private void SetBtnEnbled(string status)
        {
            string CS40000 = status;
            if (CS40000 != null)
            {
                if (!(CS40000 == "New"))
                {
                    if (CS40000 == "Edit")
                    {
                        this.btnNew.Enabled = false;
                        this.btnSave.Enabled = true;
                        this.btnEdit.Enabled = false;
                        this.btnDelete.Enabled = true;
                    }
                    else if (CS40000 == "NewLoad")
                    {
                        this.btnNew.Enabled = true;
                        this.btnSave.Enabled = false;
                        this.btnEdit.Enabled = true;
                        this.btnDelete.Enabled = false;
                    }
                }
                else
                {
                    this.btnNew.Enabled = true;
                    this.btnSave.Enabled = true;
                    this.btnEdit.Enabled = false;
                    this.btnDelete.Enabled = false;
                }
            }
        }

        private void SetTxtEnbled(string status)
        {
            string CS40000 = status;
            if (CS40000 != null)
            {
                if (!(CS40000 == "NewLoad"))
                {
                    if (CS40000 == "New")
                    {
                        this.txtParentCompany.Enabled = false;
                        this.txtDomainID.Enabled = true;
                        this.txtDominaName.Enabled = true;
                        this.txtDescription.Enabled = true;
                        this.ddlLevelNum.Enabled = true;
                    }
                    else if (CS40000 == "Edit")
                    {
                        this.txtParentCompany.Enabled = false;
                        this.txtDomainID.Enabled = false;
                        this.txtDominaName.Enabled = true;
                        this.txtDescription.Enabled = true;
                        this.ddlLevelNum.Enabled = false;
                    }
                }
                else
                {
                    this.txtParentCompany.Enabled = false;
                    this.txtDomainID.Enabled = false;
                    this.txtDominaName.Enabled = false;
                    this.txtDescription.Enabled = false;
                    this.ddlLevelNum.Enabled = false;
                }
            }
        }

        private void ShowDept()
        {
            try
            {
                DomainInfo selectedDomain = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(this.TreeView1.SelectedNode.Value, "DOMAIN") as DomainInfo;
                this.txtDomainID.Text = selectedDomain.DomainID.ToString();
                this.txtDominaName.Text = selectedDomain.Name;
                this.txtDescription.Text = selectedDomain.Description;
                this.ddlLevelNum.Text = selectedDomain.LevelNum.ToString();
                this.txtParentCompany.Text = this.hidParentText.Value;
                this.SetTxtEnbled("NewLoad");
                this.ddlDataBind(0);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            try
            {
                this.ClearData();
                TreeNode selectedNode = this.TreeView1.SelectedNode;
                this.hidSelectedNodeText.Value = selectedNode.Text;
                this.hidSelectedNodeValue.Value = selectedNode.Value;
                this.hidOldDeptCode.Value = selectedNode.Value;
                if (selectedNode.Target == "-1")
                {
                    this.hidParentText.Value = "";
                }
                else
                {
                    this.hidParentText.Value = (selectedNode.Parent == null) ? base.GetGlobalMsgResource("F1600_001") : selectedNode.Parent.Text;
                }
                this.hidParentValue.Value = (selectedNode.Parent == null) ? "1000" : selectedNode.Parent.Value;
                this.ShowDept();
                this.SetBtnEnbled("NewLoad");
            }
            catch (Exception ex)
            {
                this.lblMsg.Visible = true;
                this.lblMsg.Text = ex.Message;
            }
        }
    }
}

